<template>
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="cfpassword">
        <el-input v-model="ruleForm.cfpassword" type="password" />
      </el-form-item>
      <el-form-item label=" ">
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        Create
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
      
    </el-form>
  </template>
  
  <script setup>
  import { reactive, ref } from 'vue'

  const formSize = ref('default')
  const ruleFormRef = ref()
  const ruleForm = reactive({
    name: '',
    password: '',
    cfpassword:''
  })
  
  const rules = reactive({
    name: [
      { required: true, message: '请输入用户名', trigger:['blur','change']  },
      { pattern: /^[a-zA-Z]{1,11}$/, message: '用户名由英文字母组成，长度为1-11个字符', trigger:['blur','change'] },
    ],
    password:[
        {required: true, message: '请输入至少6位字符的密码', min:6, trigger:['blur','change']},
    ],
    cfpassword:[
        {required: true, message: '请确认密码',  trigger:['blur','change']},
        {
            validator:(rule,value,callback) => {
                console.log('rule',rule);
                console.log('value',value);
                console.log('callback',callback);
                if (value !== ruleForm.password) {
                    callback(new Error("两次密码不一致"))
                } else {
                    callback()
                }
            },
            trigger:['blur','change']
        }
    ]

  })
  
  const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
      if (valid) {
        console.log('submit!')
      } else {
        console.log('error submit!', fields)
      }
    })
  }
  

  

  </script>
  